<?php
namespace app\admin\controller;

use \think\Controller;
use \think\Db;
class Type extends BaseController
{
    public function index(){
    $types=Db::name("type")->select();
    $this->assign("types",$types);
    return $this->fetch();
    }
    public function add()
    {
        if (request()->isPost()) {
            $typename = input('typename');
        }
        $num = Db::name("type")->where("typename='$typename'")->count();
        if ($num > 0)
            $this->error("类型已存在");

        $data = ['typename' => $typename];
        $res = Db::table('type')->insert($data);
        if ($res) {
            $this->success("添加类型成功");
        } else {
            $this->error("添加失败");
        }
    }
    public function delete($id){
        $num=Db::name("perfume")->where("tid=$id")->count();
        $num1=Db::name("base")->where("tid=$id")->count();
        $num2=Db::name("clean")->where("tid=$id")->count();
        $num3=Db::name("eye")->where("tid=$id")->count();
        $num4=Db::name("face")->where("tid=$id")->count();
        $num5=Db::name("lipstick")->where("tid=$id")->count();
        $num6=Db::name("mask")->where("tid=$id")->count();
        $num7=Db::name("skin")->where("tid=$id")->count();
        $num8=Db::name("sun")->where("tid=$id")->count();
        if($num>0||$num1>0||$num2>0||$num3>0||$num4>0||$num5>0||$num6>0||$num7>0||$num8>0){
            $this->error('请先删除本分类下的信息！');
        }else{
            if(Db::name("type")->delete($id)){
                $this->success("删除成功");
            }else{
                $this->error("删除失败");
            }
        }
    }
    public function update()
    {
        $typename=null;
        $tid=null;
        if(request()->isPost())
        {
            $typename=input('param.typename');
            $tid=input('param.tid');
        }
        else
        {
            $this->error("未知错误");
        }
        $num=Db::name("type")->where("typename='$typename'")->count();
        if($num>0) $this->error("类型已经存在");
        $data=[
            'tid'=>$tid,
            'typename'=>$typename
        ];
        $res=Db::name("type")->update($data);
        if($res)
        {
            $this->success("修改类型成功");
        }
        else
        {
            $this->error("失败，遇到错误");
        }
    }
}